<template>
  <div class="large grid centered square-grid">
    <h2 class="text-xl">
      <span
        v-for="(char, index) in chars"
        :key="index"
        class="char"
        :style="{ animationDelay: `${index * 0.05}s` }"
        v-html="char === ' ' ? '&nbsp;' : char"
      ></span>
    </h2>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'

const props = defineProps<{
  text: string
}>()

const chars = computed(() => props.text.split(''))
const animationPlayed = ref(false)

onMounted(() => {
  setTimeout(() => {
    animationPlayed.value = true
  }, 2000)
})
</script>

<style scoped>
.large.grid.centered.square-grid {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 3rem 1rem; /* 增加内边距，避免动画溢出被截 */
  overflow: visible; /* 允许内容溢出容器 */
  position: relative; /* 建立定位上下文 */
  z-index: 10; /* 确保在背景元素上方 */
}

.text-xl {
  font-size: 3rem;
  font-weight: 700;
  color: currentColor;
  letter-spacing: 0.06em;
  display: inline-block;
  overflow: visible; /* 不隐藏溢出内容 */
  text-align: center;
  line-height: 1.2; /* 增加行高，避免上下拥挤 */
}

.char {
  display: inline-block;
  transform-origin: center center;
  animation: bounceIn 1.5s ease-in-out forwards;
  animation-fill-mode: both;
  opacity: 0;
}

@keyframes bounceIn {
  0% {
    transform: translateY(-60px) rotate(-360deg);
    opacity: 0;
  }
  40% {
    transform: translateY(15px) rotate(-180deg);
    opacity: 1;
  }
  70% {
    transform: translateY(-8px) rotate(-90deg);
    opacity: 1;
  }
  100% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
  }
}

/* 优化响应式适配 */
@media (max-width: 1024px) {
  .text-xl {
    font-size: 2.5rem;
  }
}
@media (max-width: 768px) {
  .text-xl {
    font-size: 2rem;
  }
}
@media (max-width: 480px) {
  .text-xl {
    font-size: 1.2rem;
    white-space: normal; /* 小屏幕允许文字换行 */
  }
}
</style>
